#include <cserver/configfile.h>

#include <fstream>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ptree.hpp>

namespace csrv{

	void loadConfig(Config& cfg, const std::string& path){
		std::ifstream sstr(path);

		using boost::property_tree::ptree;
		ptree pt;
		read_info(sstr, pt);

		cfg.config_path = path;

		cfg.aserver_site = pt.get<std::string>("aserver_site", "");
		cfg.aserver_port = pt.get<unsigned short>("aserver_port", 6001);
		cfg.cserver_addr = pt.get<std::string>("cserver_addr", "127.0.0.1");
		cfg.cserver_port = pt.get<unsigned short>("cserver_port", 8000);
		cfg.node_addr = pt.get<std::string>("node_addr", "127.0.0.1");
		cfg.node_port = pt.get<unsigned short>("node_port", 8001);
		cfg.xhs_port = pt.get<unsigned short>("xhs_port", 3404);
		cfg.echo_addr = pt.get<std::string>("echo_addr", "127.0.0.1");
		cfg.echo_port = pt.get<unsigned short>("echo_port", 8002);

		cfg.log_file = pt.get<std::string>("log_file", "/tmp/cserver.log");

		cfg.heartbeat = pt.get<int>("heartbeat", 10);
		cfg.aserver_retry_sleep = pt.get<int>("aserver_retry_sleep", 5);
		cfg.log_level = pt.get<int>("log_level", 2);
		cfg.connect_timeout = pt.get<int>("connect_timeout", 60);
		cfg.read_timeout = pt.get<int>("read_timeout", 300);
		cfg.lora_heartbeat = pt.get<int>("lora_heartbeat", 10);
		cfg.max_rest_threads = pt.get<int>("max_rest_threads", -1);

		cfg.db_string = pt.get<std::string>("db_string");
		cfg.send_sms = pt.get<std::string>("send_sms");
		cfg.sms_limit = pt.get<int>("sms_limit", 5);
		cfg.sock_path = pt.get<std::string>("sock_path", "127.0.0.1:8003");
		cfg.mobile_numbers = pt.get<std::string>("mobile_numbers");

		cfg.simple_api_config = pt.get<std::string>("simple_api_config");
		cfg.default_forward_host = pt.get<std::string>("default_forward_host", "");
		cfg.default_forward_port = pt.get<std::string>("default_forward_port", "");
	}

	void saveConfig(const Config& cfg, const std::string& path){
		boost::property_tree::ptree pt;
		
		pt.put("aserver_site", cfg.aserver_site);
		pt.put("aserver_port", cfg.aserver_port);
		pt.put("cserver_addr", cfg.cserver_addr);
		pt.put("cserver_port", cfg.cserver_port);

		pt.put("node_addr", cfg.node_addr);
		pt.put("node_port", cfg.node_port);
		pt.put("echo_addr", cfg.echo_addr);
		pt.put("node_port", cfg.echo_port);
		pt.put("xhs_port", cfg.xhs_port);

		pt.put("log_file", cfg.log_file);
		pt.put("heartbeat", cfg.heartbeat);
		pt.put("aserver_retry_sleep", cfg.aserver_retry_sleep);
		pt.put("log_level", cfg.log_level);

		pt.put("connect_timeout", cfg.connect_timeout);
		pt.put("read_timeout", cfg.read_timeout);
		pt.put("db_string", cfg.db_string);
		pt.put("send_sms", cfg.send_sms);
		pt.put("sms_limit", cfg.sms_limit);
		pt.put("sock_path", cfg.sock_path);
		pt.put("max_rest_threads", cfg.max_rest_threads);
		pt.put("mobile_numbers", cfg.mobile_numbers);

		pt.put("simple_api_config", cfg.simple_api_config);
		pt.put("default_forward_host", cfg.default_forward_host);
		pt.put("default_forward_port", cfg.default_forward_port);
	}
}
